﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.Controls.Classes;
using Silvermoon.Animations;
using System.Drawing;
using GoogleVoice;
using System.IO;

namespace GVVVM
{
    public class VoiceMailDetail : Window
    {
        private Toolbar toolbar;
        private Label lblTitle, lblStatus;       
        private BackgroundDecorator background;
        string statusText = "";
        private Button b1, b2;
        private VoiceMail voiceMail;
        private Connection gvconn;


        static VoiceMailDetail()
        {
            // The definition for the grid layout is specified here:
            /*gridDef = new GridDefinition();
            gridDef.Rows.AddRange(1f, 1f, 1f);
            gridDef.Columns.AddRange(1f, 1f, 1f);
            gridDef.CreateAutoCells();
            
            gridDef.CreateCell(0, 0).Alpha = 255;
            gridDef.CreateCell(1, 0).Alpha = 255;
            gridDef.CreateCell(2, 0);
            gridDef.CreateCell(0, 1, 2, 1);
            gridDef.CreateCell(2, 1, 1, 2);
            gridDef.CreateCell(0, 2);
            

            gridDef2 = new GridDefinition();
            gridDef2.Rows.AddRange(100, 50, 120);
            gridDef2.Columns.AddRange(2f, 80, 1f);

            gridDef2.CreateCell(1, 2).Alpha = 255;
            gridDef2.CreateCell(0, 0, 2, 1).Alpha = 255;
            gridDef2.CreateCell(2, 0).Minimized = true;
            gridDef2.CreateCell(0, 1, 2, 1).RotateY = 360;
            gridDef2.CreateCell(2, 1, 1, 2).Alpha = 32;
            gridDef2.CreateCell(0, 2).RotateX = 180;

            CellResizeMode mode = CellResizeMode.Stretched;
            foreach (GridCell c in gridDef2.Cells) c.ResizeMode = mode;
            foreach (GridCell c in gridDef.Cells) c.ResizeMode = mode;

            gridDef2.Cells[3].ResizeMode = CellResizeMode.Centered;
            gridDef.Cells[3].ResizeMode = CellResizeMode.Centered;*/
        }
        void CloseForm()
        {
            Hide(TransitionMode.ZoomOut);
        }
        public VoiceMailDetail( VoiceMail thisVoiceMail, Connection thiscon)
            : base()
        {
            this.Flags = ControlFlags.IgnoreChildClipping;
            background = new BackgroundDecorator { Color = System.Drawing.Color.Silver };
            background.Margin = new Margin(0);
            Controls.Add(background);
            gvconn = thiscon;
            //CreateGrid();
            
            voiceMail = thisVoiceMail;
            if (!voiceMail.IsDownloaded)
            {
                voiceMail.Download(gvconn.AuthToken);
                voiceMail.DownloadComplete += new EventHandler(thisVoiceMail_DownloadComplete);
                statusText = "Downloading...";
            }
            else statusText = "";

            toolbar = new Toolbar();
            Toolbar = toolbar;
            toolbar.LeftButton.Text = "Back";
            toolbar.LeftButton.Click += (s, e) => { CloseForm(); };
            string headerText = voiceMail.DisplayName + " on " + voiceMail.DisplayStartDateTime.ToShortDateString() + " at " + voiceMail.DisplayStartDateTime.ToShortTimeString();
            lblTitle = new Label { Text = headerText, Location = new Point(Screen.Width / 15, 10) }; //Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 15, FontStyle.Bold))
            Controls.Add(lblTitle);
            lblStatus = new Label { Text = statusText, Location = new Point(Screen.Width / 15, 30) };
            Controls.Add(lblStatus);
            b1 = new Button { Text = "Play", Color = Color.Blue, Location = new Point(Screen.Width / 6, Screen.Height / 6), Size = new Size(Screen.Width / 3, Screen.Height / 10) };
            b2 = new Button { Text = "Stop", Color = Color.Blue, Location = new Point((Screen.Width / 6) * 3, Screen.Height / 6), Size = new Size(Screen.Width / 3, Screen.Height / 10) };
            if (voiceMail.IsDownloaded)
            {
                b1.Enabled = true;
                b2.Enabled = true;
            }
            else
            {
                b1.Enabled = false;
                b2.Enabled = false;
            }
            b1.Click += new EventHandler(b1_Click);
            b2.Click += new EventHandler(b2_Click);
            Controls.Add(b1);
            Controls.Add(b2);

            //toolbar.Color = Color.DarkSlateGray;
            //InputBgColor = Color.DarkSlateGray;

        }

        

        void b1_Click(object sender, EventArgs e)
        {

            voiceMail.Play();
            
        }
        void b2_Click(object sender, EventArgs e)
        {
            voiceMail.Stop();
        }

        void  thisVoiceMail_DownloadComplete(object sender, EventArgs e)
        {
            try
            {
                b1.Enabled = true;
                b2.Enabled = true;
            }
            catch { }
            statusText = "Downloaded";
            lblStatus.Text = "Downloaded";
        }
 

     
        

    }
}
